<?php
/**
 * Created by PhpStorm.
 * User: osxfal
 * Date: 2019/2/23
 * Time: 下午6:06
 */
namespace app\index\controller;

use think\Controller;

/* 渠道商管理*/
class Distributor extends Controller{

    //判断用户是否登录
/*    public function __construct()
    {
        parent::__construct();
        if(empty(session('user')))
        {
            $this->error('对不起,您还没有登录!请先登录!','index/login');

        }

    }*/

    /*  一级渠道商管理 列表
        表名： go_admin  上级字段： hign_level 对应的 值 就是上级的UID
    */
    public function distributorList(){
        //参数接收
        $page = input('get.page');  //页数
        $lines = input('get.lines'); //行数

        empty($page) && $page = 1;
        empty($lines) && $lines = 20;

        $limit = ($page - 1) * $lines . ',' . $lines;

        /* 获取域名数据 */

        $res = db('domain_bind')->field('domain')->where(['domain'=>'wyq.weikj.net:88'])->find();


        /*http://wyq.weikj.net:88/?mid=1&uid=788#/landingPage*/
        $h5_pre_link = "http://" . $res['domain'] . '/';  //h5 连接前缀
        $app_pre_link = "http://" . $res['domain'] . '/app_agent?id='; //app 连接前缀
        $token_link = "http://wyq.weikj.net:86/list/t?token=";  //token 连接 前缀

        $field = "uid,mid,username,addtime,token";
        $res = db('admin')
                ->field($field)
                ->where(['mid'=>1])
                ->order('uid desc')
                ->limit($limit)
                ->select();


        $total = db('admin')->field('COUNT(uid) as t')->where(['mid'=>1])->find();


        $box = [];//初始
        if(!empty($res) && $total){
            foreach($res as &$v){
                if($v['addtime']){
                    $v['addtime'] = date('Y-m-d H:i:s',$v['addtime']);
                }

                /*http://wyq.weikj.net:88/?mid=1&uid=788#/landingPage*/
                $v['h5_link'] = $h5_pre_link . '?mid='.$v['mid'] . '&uid=' . $v['uid'] . '#/landingPage';  //h5连接地址 拼接
                $v['app_link'] = $app_pre_link . $v['uid']; //app连接地址 拼接
                $v['token_link'] = $token_link . $v['token'];  //token 连接 拼接

                $tt = $this->check_sub($v['uid']);
                $v['have_sub'] = 0;
                if($tt){
                    $v['have_sub'] = 1;
                }

            }

            $box['rows'] = $res;
            $box['total'] = $total['t'];
            $box['result'] = 1;
            $box['tips'] = '查询成功！';
//            dump($box);die;

            $box = json_encode($box,320);

            exit($box);
        }//if
        elseif(empty($res)){
            $box['rows'] = [];
            $box['total'] = 0;
            $box['result'] = 1;
            $box['tips'] = '查询成功！但是并没有匹配的数据呢~';

            $box = json_encode($box,320);

            exit($box);

        }
        else{

            $box['result'] = 2;
            $box['tips'] = '查询失败';

            $box = json_encode($box,320);
            exit($box);
        }

    }


    /* 2级 渠道商*/
    public function get_sub_distributor(){
        $uid = input('get.uid');

        $where = [];
        if($uid){
            $uid = trim($uid);
            $where['hign_level'] = $uid;
        }

        empty($page) && $page = 1;
        empty($lines) && $lines = 20;

        $limit = ($page - 1) * $lines . ',' . $lines;

        /* 获取域名数据 */

        $res = db('domain_bind')->field('domain')->where(['domain'=>'wyq.weikj.net:88'])->find();


        $h5_pre_link = "http://" . $res['domain'] . '/';  //h5 连接前缀
        $app_pre_link = "http://" . $res['domain'] . '/app_agent?id='; //app 连接前缀
        $token_link = "http://wyq.weikj.net:86/list/t?token=";  //token 连接 前缀


        $field = "uid,mid,username,addtime,token";
        $res = db('admin')
            ->field($field)
            ->where($where)
            ->order('uid desc')
            ->limit($limit)
            ->select();


        $total = db('admin')->field('COUNT(uid) as t')->where($where)->find();


        $box = [];//初始
        if(!empty($res) && $total) {
            foreach ($res as &$v) {
                if ($v['addtime']) {
                    $v['addtime'] = date('Y-m-d H:i:s', $v['addtime']);
                }

                $v['h5_link'] = $h5_pre_link . '?mid='.$v['mid'] . '&uid=' . $v['uid'] . '#/landingPage';  //h5连接地址 拼接
                $v['app_link'] = $app_pre_link . $v['uid']; //app连接地址 拼接
                $v['token_link'] = $token_link . $v['token'];

            }

            $box['rows'] = $res;
            $box['total'] = $total['t'];
            $box['result'] = 1;
            $box['tips'] = '查询成功！';


            $box = json_encode($box, 320);

            exit($box);
        }

    }//get_sub func

    //查是否有二级
    public function check_sub($uid){
        $res = db('admin')->where(['hign_level'=>$uid])->limit('1')->find();
        if(!empty($res)){
            return true;
        }else{
            return false;
        }
    }


    /*获取对应渠道商的渠道列表
        说明： 通过uid 查询其对应的渠道列表
    */
    public function getMyGroup(){
        //接收 uid 参数
        $uid = input('post.uid');//渠道商id

        $res = db('admin')->where(['hign_level'=>$uid])->order('id desc')->select();
        $chanellist = json_encode($res);
        return $chanellist;

    }


    /*渠道商管理 修改渠道商用户名，密码*/
    public function changePwd(){
        //接收 uid 参数
        $uid = input('post.uid');//渠道商id
        $username = input('post.username');
        $userpass = input('post.userpass');
        $c_userpass = input('post.c_userpass');//确认密码

        //比对两次输入的密码
        if($userpass == $c_userpass){
            $data = [];
            $data['username'] = $username;
            $data['userpass'] = $userpass;

            $res = db('admin')->where(['uid'=>$uid])->update($data);

            $msg = [];
            if($res !== false){ //更新成功时
                $msg['result'] = 1;
                $msg['tips'] = '更新账号密码成功！';
//                $this->success('更新账号密码成功！',url('distributorList'));
            }else{
                $msg['result'] = 2;
                $msg['tips'] = '更新账号密码失败！请稍后再试~';
//                $this->error('更新失败！');
            }
        }//if
        else{
            $msg['result'] = 2;
            $msg['tips'] = '两次输入密码不一样！';
//            $this->error('两次输入密码不一样！');
        }

        exit(json_encode($msg,320));

    }//func changePwd


    /*渠道商添加*/
    public function distributorAdd(){
        //接收 参数

        $username = input('post.username');

        /*  先判断是否有此用户， 避免重复输入 */
        $username = trim($username);
        if($this->check_duplicate($username)){
            $box['result'] =2;
            $box['tips'] = '该渠道商已经存在!';
            $box = json_encode($box,320);
            exit($box);
        }

        $userpass = input('post.userpass');
        $c_userpass = input('post.c_userpass');//确认密码
        $email = input('post.email');//email

        //渠道商下拉菜单 写死一个。 不用处理

        if($userpass == $c_userpass){
            $data = [];
            $data['username'] = trim($username);
            $data['userpass'] = md5(trim($userpass));
            $data['useremail'] = trim($email);
            $data['mid'] = 1; //渠道商一级， 新增的都默认， 写死
            $data['token'] = md5($username);

            $res = db('admin')->insert($data);
            $box = []; //初始化 消息盒子 用来将结果信息返回给前端
            if($res){
               $box['result'] =1;
               $box['tips'] = '成功添加渠道商';

               $box = json_encode($box,320);
               exit($box);
                // $this->success('添加渠道商成功！',url('distributorList'));
            }else{
                // $this->error('添加渠道商失败！');
                $box['result'] =2;
                $box['tips'] = '添加渠道商失败!';
                $box = json_encode($box,320);
                exit($box);
            }
        }
        else{
           
                $box['result'] =2;
                $box['tips'] = '两次输入密码不一样！!';
                $box = json_encode($box,320);
                exit($box);
            // $this->error('两次输入密码不一样！');
        }

    }


    /*检查是否提交重名的 渠道商*/
    public function check_duplicate($name){
        $res = db('admin')->where(['username'=>$name])->find();
        if(!empty($res)){
            return true;
        }
        else{
            return false;
        }
    }


    /*渠道商 删除*/
    public function del_distributor(){
        //接收 uid 参数
        $uid = input('post.id');//渠道商id

        db('admin')->delete(['uid'=>$uid]);
        $box = [];
        $box['result'] =1;
        $box['tips'] = '删除渠道商成功！!';
        $box = json_encode($box,320);
        exit($box);
        // $this->success('删除渠道商成功！',url('distributorList'));
    }
}